
import axios from 'axios'
import router from '../router/index.js'
let { baseUrl } = window.globalData
let permission = function (res) {
    let mess = {...res}
    if (mess.response) {
        switch (mess.response.status) {
        case 400:
            //router.push('/login')
            // window.location.reload()
            return true
        case 401:
            console.log(mess.response.data.message)
            //router.push('/login')
            // window.location.reload()
            return true
        case 403:
            console.log(mess.response.data.message)
            //router.push('/login')
            // window.location.reload()
            return true
        case 404:
            // delCookie('login_tokens')
            // router.push('/login')
            console.log(mess)
            console.log(mess.response.data.message)
            // window.location.reload()
            return true
        default:
            // delCookie('login_tokens')
            router.push('/')
            // window.location.reload()
            return false
        }
    }
    if (res.message === 'timeout of 20000ms exceeded' || res.message === 'timeout of 20000ms exceeded/network error') {
        // delCookie('login_tokens')
        console.log(res.message)
        // router.push('/')
        return true
    }
    return false
}

let request = axios.create({
    baseURL: baseUrl,
    timeout: 20000,
    headers: {
        'Content-Type': 'application/json;charset=utf-8'
    }
})


request.interceptors.response.use(
    response => {
        if (response.status == 200) {
            return response
        }
    },
    error => {
        if (!permission(error)) {
            return Promise.reject(error)
        }
    }
)

export {
    request,
    baseUrl,
}
